<template>
  <div style="padding-top:15px;">
    <div class="coupon-con" v-for="(item,index) in gain_coupons" :key="index">
      <div style="width:70%;padding-left:10px;">
        <div style="font-size:16px;">{{item.coupon_tpl_name}}</div>
        <!--<div style="font-size:12px;">满30元可用</div>-->
      </div>
      <div class="gain-btn" @click="gainCoupon(item)">{{item.status}}</div>
    </div>
  </div>
</template>
<script>
import api from '@/utils/api'
//import moment from 'moment'
import {formatDate } from '@/utils/index'
import { mapState, mapActions, mapGetters } from 'vuex'
export default {
  data () {
    return {
      gain_coupons:[],
    }
  },
  computed: {
    ...mapGetters([
      'login_info',
      'user'
    ])
  },
  beforeMount() {
    this.gain_coupons = []
    console.log('beforeMount')
    if (!this.login_info || !this.login_info.brand_id|| !this.login_info.openid) {
      // 用户未授权
      let url = 'get_user_info?callback_url=coupon_center'
      wx.redirectTo({
        url: url
      })
      return
    }
    if (!this.user || !this.user.id) {
      // 用户未授权
      let url = 'get_user_info?callback_url=coupon_center'
      wx.redirectTo({
        url: url
      })
      return
    }
  },
  async mounted () {
    let result = await api.getMemberDay({brand_id:this.login_info.brand_id})
    if (result.code == 0) {
      let member_day_coupon = result.data
      var effective_time = this.computeEffectiveTime(member_day_coupon)
      let form = {
        brand_id:member_day_coupon.brand_id,
        card_no:this.user.card_no,
        coupon_tpl_id:member_day_coupon.coupon_tpl_id,
        begin_time:effective_time.begin_time,
        end_time:effective_time.end_time,
        active_type:'member_day'
      }
      let res = await api.judgeOwnCoupon(form)
      if (res.code == 0) {
        if (res.data.id == 0) {
          this.$set(
            member_day_coupon,"status","立即领取"
          )
        }else {
          this.$set(
            member_day_coupon,"status","已领取"
          )
        }
      }
      this.gain_coupons.push(member_day_coupon)
    }
  },
  methods: {
    //计算有效时间
    computeEffectiveTime(item){
      var myDate = new Date()
      var year = myDate.getFullYear()
      var memberDay
      if (item.member_day_type == 'year') {
        memberDay = year + '-' + item.member_day
      }else if (item.member_day_type == 'month') {
        var month = myDate.getMonth()+1
        memberDay = year + '-' + month + '-' + item.member_day
      }else {
        //var nowWeek = moment(myDate).format('d')
        //1到7，7表示周日，6表示周六
         var nowWeek = new Date().getDay()
        if (nowWeek == 0) {
          nowWeek = 7
        }
        if (item.member_week - nowWeek == 0) {
          //memberDay = moment(myDate).format('YYYY-MM-DD')
          memberDay = formatDate(new Date().getTime())
        }else if (item.member_week - nowWeek == 1) {
          //memberDay = moment(myDate).add('days',1).format('YYYY-MM-DD')
          memberDay = formatDate(new Date().getTime()+ 24*60*60)
        }else if (item.member_week - nowWeek == 2) {
          //memberDay = moment(myDate).add('days',2).format('YYYY-MM-DD')
          memberDay = formatDate(new Date().getTime()+ 2*24*60*60)
        }else if (item.member_week - nowWeek == -5) {//会员日是星期一
          //memberDay = moment(myDate).add('days',2).format('YYYY-MM-DD')
          memberDay = formatDate(new Date().getTime()+ 2*24*60*60)
        }else if (item.member_week - nowWeek == -6) {//会员日是星期日
          //memberDay = moment(myDate).add('days',1).format('YYYY-MM-DD')
          memberDay = formatDate(new Date().getTime()+ 24*60*60)
        }
      }
      //var begin_time = moment(memberDay).valueOf()
      //var end_time = moment(memberDay).add('days',1).valueOf()-1
      var begin_time = new Date(memberDay).valueOf()
      var end_time = new Date(formatDate(new Date(memberDay).getTime()+ 24*60*60)).valueOf()-1
      return {begin_time:begin_time,end_time:end_time}
    },
    async gainCoupon(item){
      if (item.status == "已领取") {
        wx.showToast({
          title: '该优惠券已领取',
          icon: 'none'
        })
        return
      }
      var effective_time = this.computeEffectiveTime(item)
      let form = {
        brand_id:item.brand_id,
        card_no:this.user.card_no,
        coupon_tpl_id:item.coupon_tpl_id,
        begin_time:effective_time.begin_time,
        end_time:effective_time.end_time
      }
      console.log('form=>',form)
      let result = await api.gainCoupon(form)
      console.log('result=>',result)
      if (result.code == 0) {
        wx.showToast({
          title: '领取成功',
          icon: 'none'
        })
        item.status = "已领取"
      }
    }
  }
}
</script>

<style >
page{
  background-color: #EAEAEA;
}
.coupon-con{
  display:flex;
  align-items:center;
  height:90px;
  margin: 0 20px 15px 20px;
  background:white;
  color:#B4995F;
  border-radius: 5px;
  overflow: hidden;
}
.gain-btn{
  width:30%;
  height:100%;
  line-height: 90px;
  text-align:center;
  background:#696969;
  color:white;
}
</style>
